<?php
namespace app\logic;

use app\models\Answer;
use app\models\User;
use yii\db\Query;


class UserLogic
{
    public function getAllStudent($query=[], $page=1, $limit = 10){
        if(isset($query['user_status']) && $query['user_status']==-1)
            unset($query['user_status']);
        if(isset($query['nick_name']) && $query['nick_name'])
            $query['nick_name'] = base64_encode($query['nick_name']);
        $user = User::find()->filterWhere($query)->orderBy('user_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $user->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $user->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'student/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getUser($query = [], $page = 1, $limit = 10)
    {
        $user = User::find()->filterWhere($query)->orderBy('user_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $user->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $user->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'user/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function addUser($params){
        if(User::findOne(['phone'=>$params['phone']])){
            return false;
        }else{
            $user = new User();
            $user->phone = $params['phone'];
            $user->type = $params['type'];
            $user->create_time = date('Y-m-d H:i:s');
            $user->status = 0;
            return $user->save();
        }
    }


    public function saveStatus($id)
    {
        $user = User::findOne($id);
        if($user->status)
            $user->status = 0;
        else
            $user->status = 1;
        return $user->save();
    }



    public function getById($user_id){
        return User::findOne($user_id)->toArray();
    }

    public function setPassword($user_id,$username,$password){
        $user = User::findOne($user_id);
        $user->username = $username;
        $user->password = $password;
        $user->user_status = 1;
        return $user->save();
    }

    public function getAnswer($user_id,$query=[], $page=1, $limit = 10){
        if(isset($query['user_id']))
            $user_id = $query['user_id'];
        $subQuery = new \yii\db\Query();
        $subQuery->select('sum(is_correct) as sum_correct')
            ->from('{{%answer}} a')
            ->leftJoin('{{%test}} t','t.test_id=a.test_id')
            ->leftJoin('{{%paper}} p','p.paper_id=a.paper_id')
            ->where(['a.user_id'=>$user_id]);
        if(isset($query['test_type']) && $query['test_type']!=0){
            $subQuery->andWhere(['t.test_type'=>$query['test_type']]);
        }
        if(isset($query['paper_type']) && $query['paper_type']!=0){
            $subQuery->andWhere(['p.paper_type'=>$query['paper_type']]);
        }
        if(isset($query['start_time']) && $query['start_time']!=''){
            $start_time = $query['start_time']." 00:00:00";
            $subQuery->andWhere("answer_time>='$start_time'");
        }
        if(isset($query['end_time']) && $query['end_time']!=''){
            $end_time = $query['end_time']." 23:59:59";
            $subQuery->andWhere("answer_time<='$end_time'");
        }
        $subQuery->groupBy('a.answer_id');
        $is_correct = Answer::find()->from(['temp'=>$subQuery])->where('sum_correct=1')->count();
        $not_correct = Answer::find()->from(['temp'=>$subQuery])->where('sum_correct=0')->count();
        $answer = Answer::find()
            ->select('a.*,t.test_type,p.paper_type,t.test_title,t.test_answer')
            ->from('{{%answer}} a')
            ->leftJoin('{{%test}} t','t.test_id=a.test_id')
            ->leftJoin('{{%paper}} p','p.paper_id=a.paper_id')
            ->where(['a.user_id'=>$user_id]);
        if(isset($query['test_type']) && $query['test_type']!=0){
            $answer->andWhere(['t.test_type'=>$query['test_type']]);
        }
        if(isset($query['paper_type']) && $query['paper_type']!=0){
            $answer->andWhere(['p.paper_type'=>$query['paper_type']]);
        }

        if(isset($query['start_time']) && $query['start_time']!=''){
            $start_time = $query['start_time']." 00:00:00";
            $answer->andWhere("answer_time>='$start_time'");
        }
        if(isset($query['end_time']) && $query['end_time']!=''){
            $end_time = $query['end_time']." 23:59:59";
            $answer->andWhere("answer_time<='$end_time'");
        }
        $answer->orderBy('answer_time desc')->offset(($page - 1) * $limit)->limit($limit);
        $lists = $answer->asArray()->all();
        $query['user_id'] = $user_id;
        $return = [
            'is_correct'=>$is_correct,
            'not_correct'=>$not_correct,
            'lists' => $lists,
            'pager' => [
                'total' => $answer->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => "user/answer",
                'query' => $query
            ]
        ];
        return $return;
    }



}
